@extends('admin.layouts.app')

@section('right-content')

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>编辑用户</legend>
    </fieldset>

    <form class="layui-form" action="">
        {{ method_field('PUT') }}
        <div class="layui-form-item">
            <label class="layui-form-label">用户名称</label>
            <div class="layui-input-block">
                <input type="text" name="user_name" value="{{ $user->user_name }}" lay-verify="required" autocomplete="off" placeholder="请输入用户名称" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">手机号码</label>
            <div class="layui-input-block">
                <input type="text" name="phone" value="{{ $user->phone }}" lay-verify="phone" autocomplete="off" placeholder="请输入手机号码" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="email" value="{{ $user->email }}" lay-verify="email" autocomplete="off" placeholder="请输入邮箱地址" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">用户描述</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" class="layui-textarea" name="description" >{{ $user->description }}</textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">所属角色</label>
            <div class="layui-input-block">
                @foreach ($roles as $role)
                    <input type="checkbox" name="roles[]" lay-skin="primary" value="{{ $role->id }}" @if (in_array($role->id, $user->role_ids->all())) checked @endif title="{{ $role->title }}">
                @endforeach
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="启用" @if ($user->status == 1) checked @endif>
                <input type="radio" name="status" value="0" title="停用" @if ($user->status == 0) checked @endif>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="sub" data-url="{{ route('admin.users.update', ['id' => $user->id]) }}">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
@endsection

@section('my-js')
<script type="text/javascript">
   layui.use('form', function(){
       var form = layui.form;

       form.verify({
        phone: function(value, item) {
            if (value != '' && !/^\d{11}$/.test(value)) {
                return '手机号码不正确';
            }
        },
        email: function(value, item) {
            if (value != '' && !/^\w+@\w+\.(cn|com)$/.test(value)) {
                return '邮箱不正确';
            }
        }
        });

      form.on('submit(sub)', function(data){
        var url = $(this).attr('data-url');
        commonAjaxSubmit(url, 'PUT');

      })
    });
</script>
@endsection

